﻿using Common.Communal;
using ERP.Domain.CustomerSupplier;
using ERP.Domain.SalesManagement;
using MediatR;
using Sell.Api.Read.Application.Command.PhraseCommand;
using Sell.DTO;
using SqlSugar;

namespace Sell.Api.Read.Application.CommandHandlers.PhraseCommandHandlers
{
    public class PhraselistCommandHandlers : IRequestHandler<PhraselistCommand, ApiResult<ApiPaging<Phraselistdto>>>
    {
        public ISqlSugarClient SqlSugar { get; }
        public PhraselistCommandHandlers(ISqlSugarClient sqlSugar)
        {
            SqlSugar = sqlSugar;
        }
        /// <summary>
        /// 销售合同列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<ApiPaging<Phraselistdto>>> Handle(PhraselistCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<Phraselistdto>> res = new ApiResult<ApiPaging<Phraselistdto>>();
            res.Code = EnumCode.Ok;
            res.Message = "销售合同显示成功";
            /// 初始化查询：关联Phrase表和Customer表
            var query = SqlSugar.Queryable<Phrase>()
                .LeftJoin<Customer>((p, c) => p.CustomerId==c.Id)
                .Where(p => p.IsDelete == false).Select(
                (p,c) => new Phraselistdto
                {
                    ContractCode=p.ContractCode,
                     CustomerName=p.ContractName,
                      ContractName=p.ContractName,
                       CustomerId=c.Id,
                        CustomerCode=c.CustomerCode,
                         CreateTime=p.CreateTime,
                          ContractPrice=p.ContractPrice,
                           CustomerType=c.CustomerType,
                            Desc=p.Desc,
                             Id=p.Id,
                              IsDelete=p.IsDelete,
                               Phone=c.Phone,
                                ProductId=p.ProductId,
                                 Review=p.Review,
                                   State=c.State,
                                    UpdateTime=p.UpdateTime,
                                   UserPeople=c.UserPeople,
                });
            if (!string.IsNullOrEmpty(request.ContractName))
            {
                query = query.Where(p => p.ContractName.Contains(request.ContractName));
            }
            if (request.CustomerId != null)
            {
                query = query.Where(p => p.CustomerId.Equals(request.CustomerId));
            }
            //SqlSugar分页查询实现
            ApiPaging<Phraselistdto> result = new ApiPaging<Phraselistdto>();
            result.TotalCount = query.Count();//查询总记录数
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.pagesize);//计算总页数
            result.Data = query.Skip((request.pageindex - 1) * request.pagesize)//跳过前面的记录
                .Take(request.pagesize)//取指定页大小的记录
                .ToList();//执行查询并转为List
            res.Success(result, "查询成功");
            return res;
        }
    }
}
